6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
16 protected function tearDown() {
17 $_SERVER = $this->oldServer
;
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
26 * @covers WebRequest::detectProtocol
28 public function testDetectServer( $expected, $input, $description ) {
29 $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
32 $result = WebRequest
::detectServer();
33 $this->assertEquals( $expected, $result, $description );
36 public static function provideDetectServer() {
51 'Host header with secure'
59 'Default SERVER_PORT',
73 'HTTP_X_FORWARDED_PROTO' => 'https',
82 'SERVER_PORT' => '81',
83 'HTTP_X_FORWARDED_PROTO' => 'https',
100 'Host server name precedence'
105 'HTTP_HOST' => '[::1]',
106 'SERVER_NAME' => '::1',
107 'SERVER_PORT' => '81',
114 'SERVER_NAME' => '[2001'
116 'Kind of like lighttpd per commit message in MW r83847',
119 'http://[2a01:e35:2eb4:1::2]:777',
121 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
123 'Possible lighttpd environment per bug 14977 comment 13',
128 protected function mockWebRequest( $data = [] ) {
129 // Cannot use PHPUnit getMockBuilder() as it does not support
130 // overriding protected properties afterwards
131 $reflection = new ReflectionClass( 'WebRequest' );
132 $req = $reflection->newInstanceWithoutConstructor();
134 $prop = $reflection->getProperty( 'data' );
135 $prop->setAccessible( true );
136 $prop->setValue( $req, $data );
138 $prop = $reflection->getProperty( 'requestTime' );
139 $prop->setAccessible( true );
140 $prop->setValue( $req, microtime( true ) );
146 * @covers WebRequest::getElapsedTime
148 public function testGetElapsedTime() {
149 $req = $this->mockWebRequest();
150 $this->assertGreaterThanOrEqual( 0.0, $req->getElapsedTime() );
151 $this->assertEquals( 0.0, $req->getElapsedTime(), '', /*delta*/ 0.2 );
155 * @covers WebRequest::getVal
156 * @covers WebRequest::getGPCVal
157 * @covers WebRequest::normalizeUnicode
159 public function testGetValNormal() {
160 // Assert that WebRequest normalises GPC data using UtfNormal\Validator
161 $input = "a \x00 null";
162 $normal = "a \xef\xbf\xbd null";
163 $req = $this->mockWebRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] );
164 $this->assertSame( $normal, $req->getVal( 'x' ) );
165 $this->assertNotSame( $input, $req->getVal( 'x' ) );
166 $this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) );
170 * @covers WebRequest::getVal
171 * @covers WebRequest::getGPCVal
173 public function testGetVal() {
174 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] );
175 $this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' );
176 $this->assertSame( null, $req->getVal( 'z' ), 'Not found' );
177 $this->assertSame( null, $req->getVal( 'y' ), 'Array is ignored' );
178 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
182 * @covers WebRequest::getRawVal
184 public function testGetRawVal() {
185 $req = $this->mockWebRequest( [
190 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
191 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
192 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
193 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
197 * @covers WebRequest::getArray
199 public function testGetArray() {
200 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] );
201 $this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' );
202 $this->assertSame( null, $req->getArray( 'z' ), 'Not found' );
203 $this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) );
207 * @covers WebRequest::getIntArray
209 public function testGetIntArray() {
210 $req = $this->mockWebRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] );
211 $this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' );
212 $this->assertSame( null, $req->getIntArray( 'z' ), 'Not found' );
213 $this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) );
217 * @covers WebRequest::getInt
219 public function testGetInt() {
220 $req = $this->mockWebRequest( [
227 $this->assertSame( 0, $req->getInt( 'x' ), 'Text' );
228 $this->assertSame( 0, $req->getInt( 'y' ), 'Array' );
229 $this->assertSame( 0, $req->getInt( 'z' ), 'Not found' );
230 $this->assertSame( 0, $req->getInt( 'zero' ) );
231 $this->assertSame( 4, $req->getInt( 'answer' ) );
232 $this->assertSame( -2, $req->getInt( 'neg' ) );
236 * @covers WebRequest::getIntOrNull
238 public function testGetIntOrNull() {
239 $req = $this->mockWebRequest( [
246 $this->assertSame( null, $req->getIntOrNull( 'x' ), 'Text' );
247 $this->assertSame( null, $req->getIntOrNull( 'y' ), 'Array' );
248 $this->assertSame( null, $req->getIntOrNull( 'z' ), 'Not found' );
249 $this->assertSame( 0, $req->getIntOrNull( 'zero' ) );
250 $this->assertSame( 4, $req->getIntOrNull( 'answer' ) );
251 $this->assertSame( -2, $req->getIntOrNull( 'neg' ) );
255 * @covers WebRequest::getFloat
257 public function testGetFloat() {
258 $req = $this->mockWebRequest( [
265 $this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' );
266 $this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' );
267 $this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' );
268 $this->assertSame( 0.0, $req->getFloat( 'zero' ) );
269 $this->assertSame( 4.2, $req->getFloat( 'answer' ) );
270 $this->assertSame( -2.0, $req->getFloat( 'neg' ) );
274 * @covers WebRequest::getBool
276 public function testGetBool() {
277 $req = $this->mockWebRequest( [
284 $this->assertSame( true, $req->getBool( 'x' ), 'Text' );
285 $this->assertSame( false, $req->getBool( 'y' ), 'Array' );
286 $this->assertSame( false, $req->getBool( 'z' ), 'Not found' );
287 $this->assertSame( false, $req->getBool( 'zero' ) );
288 $this->assertSame( true, $req->getBool( 'f' ) );
289 $this->assertSame( true, $req->getBool( 't' ) );
292 public static function provideFuzzyBool() {
295 [ '', false, '(empty string)' ],
308 * @dataProvider provideFuzzyBool
309 * @covers WebRequest::getFuzzyBool
311 public function testGetFuzzyBool( $value, $expected, $message = null ) {
312 $req = $this->mockWebRequest( [ 'x' => $value ] );
313 $this->assertSame( $expected, $req->getFuzzyBool( 'x' ), $message ?
: "Value: '$value'" );
317 * @covers WebRequest::getFuzzyBool
319 public function testGetFuzzyBoolDefault() {
320 $req = $this->mockWebRequest();
321 $this->assertSame( false, $req->getFuzzyBool( 'z' ), 'Not found' );
325 * @covers WebRequest::getCheck
327 public function testGetCheck() {
328 $req = $this->mockWebRequest( [ 'x' => 'Value', 'zero' => '0' ] );
329 $this->assertSame( false, $req->getCheck( 'z' ), 'Not found' );
330 $this->assertSame( true, $req->getCheck( 'x' ), 'Text' );
331 $this->assertSame( true, $req->getCheck( 'zero' ) );
335 * @covers WebRequest::getText
337 public function testGetText() {
338 // Avoid FauxRequest (overrides getText)
339 $req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] );
340 $this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' );
344 * @covers WebRequest::getValues
346 public function testGetValues() {
347 $values = [ 'x' => 'Value', 'y' => '' ];
348 // Avoid FauxRequest (overrides getValues)
349 $req = $this->mockWebRequest( $values );
350 $this->assertSame( $values, $req->getValues() );
351 $this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' );
355 * @covers WebRequest::getValueNames
357 public function testGetValueNames() {
358 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => '' ] );
359 $this->assertSame( [ 'x', 'y' ], $req->getValueNames() );
360 $this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' );
364 * @dataProvider provideGetIP
365 * @covers WebRequest::getIP
367 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
369 $this->setMwGlobals( [
370 'wgSquidServersNoPurge' => $squid,
371 'wgUsePrivateIPs' => $private,
373 'IsTrustedProxy' => [
374 function ( &$ip, &$trusted ) use ( $xffList ) {
375 $trusted = $trusted ||
in_array( $ip, $xffList );
382 $request = new WebRequest();
383 $result = $request->getIP();
384 $this->assertEquals( $expected, $result, $description );
387 public static function provideGetIP() {
392 'REMOTE_ADDR' => '127.0.0.1'
402 'REMOTE_ADDR' => '::1'
412 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
413 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
415 [ 'ABCD:1:2:3:4:555:6666:7777' ],
423 'REMOTE_ADDR' => '12.0.0.1',
424 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
426 [ '12.0.0.1', '12.0.0.2' ],
429 'With X-Forwaded-For'
434 'REMOTE_ADDR' => '12.0.0.1',
435 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
440 'With X-Forwaded-For and disallowed server'
445 'REMOTE_ADDR' => '12.0.0.1',
446 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
451 'With multiple X-Forwaded-For and only one allowed server'
456 'REMOTE_ADDR' => '12.0.0.2',
457 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
459 [ '12.0.0.1', '12.0.0.2' ],
462 'With X-Forwaded-For and private IP (from cache proxy)'
467 'REMOTE_ADDR' => '12.0.0.2',
468 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
470 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
473 'With X-Forwaded-For and private IP (allowed)'
478 'REMOTE_ADDR' => '12.0.0.2',
479 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
481 [ '12.0.0.1', '12.0.0.2' ],
484 'With X-Forwaded-For and private IP (allowed)'
489 'REMOTE_ADDR' => '12.0.0.2',
490 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
492 [ '12.0.0.1', '12.0.0.2' ],
495 'With X-Forwaded-For and private IP (disallowed)'
500 'REMOTE_ADDR' => '12.0.0.1',
501 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
504 [ '12.0.0.1', '12.0.0.2' ],
506 'With X-Forwaded-For'
511 'REMOTE_ADDR' => '12.0.0.1',
512 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
517 'With multiple X-Forwaded-For and only one allowed server'
522 'REMOTE_ADDR' => '12.0.0.2',
523 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
528 'With X-Forwaded-For and private IP and hook (disallowed)'
533 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
534 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
536 [ 'ABCD:1:2:3::/64' ],
544 'REMOTE_ADDR' => '12.0.0.1',
545 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
556 * @expectedException MWException
557 * @covers WebRequest::getIP
559 public function testGetIpLackOfRemoteAddrThrowAnException() {
560 // ensure that local install state doesn't interfere with test
561 $this->setMwGlobals( [
562 'wgSquidServersNoPurge' => [],
563 'wgSquidServers' => [],
564 'wgUsePrivateIPs' => false,
568 $request = new WebRequest();
569 # Next call throw an exception about lacking an IP
573 public static function provideLanguageData() {
575 [ '', [], 'Empty Accept-Language header' ],
576 [ 'en', [ 'en' => 1 ], 'One language' ],
577 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
580 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
581 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
585 [ 'es' => 1, 'en' => '0.5' ],
586 'Spanish as first language and English and second'
588 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
589 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
590 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
591 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
593 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
594 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
595 'Preference for Romance languages'
599 [ 'en-gb' => 1, 'en-us' => '1' ],
600 'Two equally prefered English variants'
602 [ '_', [], 'Invalid input' ],
607 * @dataProvider provideLanguageData
608 * @covers WebRequest::getAcceptLang
610 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
611 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
612 $request = new WebRequest();
613 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );